// $Id: DbTransaction.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 NourY Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
#pragma warning disable 1587
/** \file
 * \brief Definition of a transaction adapter.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */
#pragma warning restore 1587

using System;
using System.Data;
using System.Data.Common;

namespace Salamanca.DataAccess.Data
{



    /// <summary>Interface of a generic transaction.</summary>
    public interface ITransaction:
        IDbTransaction
    {

        /// <summary>Event triggered when the transaction is committed.</summary>
        event EventHandler Committed;
        /// <summary>Evenet triggered when the transaction is disposed</summary>
        event EventHandler Disposed;
    }



    /// <summary>Database connection adapter.</summary>
    /// <remarks></remarks>
    public sealed class DbTransactionAdapter:
        ITransaction
    {

        /// <summary>Constructor.</summary>
        private DbTransactionAdapter()
        {
            throw new InvalidOperationException();
        }

        /// <summary>Constructor.</summary>
        internal DbTransactionAdapter(DbTransaction transaction)
        {
            _Transaction=transaction;
        }

        /// <summary>Finalizer</summary>
        ~DbTransactionAdapter()
        {
            Dispose(false);
        }

        /// <summary>Commits the database transaction.</summary>
        public void Commit()
        {
            _Transaction.Commit();
            if (Committed!=null)
                Committed(this, new EventArgs());
        }

        /// <summary>Releases the unmanaged resources used by the <see cref="DbTransactionAdapter" /> and optionally releases the managed resources.</summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>Rolls back a transaction from a pending state.</summary>
        public void Rollback()
        {
            _Transaction.Rollback();
        }

        /// <summary>Releases the unmanaged resources used by the <see cref="DbTransactionAdapter" /> and optionally releases the managed resources.</summary>
        private void Dispose(bool disposing)
        {
            if (disposing)
                _Transaction.Dispose();

            if (Disposed!=null)
                Disposed(this, new EventArgs());
        }

        /// <summary>Connection.</summary>
        public DbConnection Connection
        {
            get
            {
                return _Transaction.Connection;
            }
        }

        /// <summary>Specifies the <see cref="IsolationLevel" /> for this transaction.</summary>
        public IsolationLevel IsolationLevel
        {
            get
            {
                return _Transaction.IsolationLevel;
            }
        }

        internal DbTransaction DbTransaction
        {
            get
            {
                return _Transaction;
            }
        }

        /// <summary>Connection.</summary>
        IDbConnection IDbTransaction.Connection
        {
            get
            {
                return Connection;
            }
        }

        /// Transaction.
        private DbTransaction _Transaction;

        /// <summary>Event triggered when the transaction is committed.</summary>
        public event EventHandler Committed;
        /// <summary>Evenet triggered when the transaction is disposed</summary>
        public event EventHandler Disposed;
    }

}
